<?php
/**
 * This class will generate a set of selectboxes to select a date
 *
 * @author Herjan Treurniet
 * @since 2008-12-21
 * @package Form.Elements
 */
class SelectDate extends FormElement {

	/**
	 * Constructor
	 *
	 * @param string $name
	 * @param mixed $value
	 * @param array $properties
	 */
	public function __construct( $name, $value = null, array $properties = array() ) {
		if ( empty ( $value ) ) {
			$value = date ( 'Y-m-d' );
		}

		$this->value = $this->getDateArray ( $value );

		parent::__construct ( $name, $properties );
	}

	public static function getTimeStamp( array $dateTime ) {
		if ( isset( $dateTime['month'], $dateTime['day'], $dateTime['year'] ) ) {
			return mktime ( 0, 0, 0, $dateTime['month'], $dateTime['day'], $dateTime['year'] );
		}

		return 0;
	}

	public static function getDate ( array $date = null ) {
		if ( is_array ( $date ) ) {
			$date = self::getTimeStamp ( $date );
		} else {
			$date = time();
		}

		return date ( 'Y-m-d', $date );
	}

	public function getDateArray ( ) {
		$args = func_get_args ( );

		if ( is_array ( $args [0] ) ) {
			$a = $args [0];
			$string = self::getDate($a);
		} elseif ( count ( $args ) == 3 ) {
			$string = $args[0].'-'.$args[1].'-'.$args[2];
		} else {
			$string = $args [0];
		}

		$date = strtotime ( $string );

		$array = array (
			'year' => date ( 'Y', $date ),
			'month' => date ( 'm', $date ),
			'day' => date ( 'd', $date )
		);

		if ( checkdate ( $array['month'], $array['day'], $array['year'] ) ) {
			return $array;
		} else {
			return array (
				'year' => date ( 'Y' ),
				'month' => date ( 'm' ),
				'day' => date ( 'd' )
			);
		}
	}

	/**
	 * @see FormNode::__toString()
	 */
	public function __toString( ) {
		$select = '<div class="form date"><label>' . $this->getProperty("label") . '</label>';

		$selected = $this->value;

		$selects = array (
			'day' => range ( 1, 31 ),
			'month' => array ( '##january##', '##february##', '##march##', '##april##', '##may##', '##june##', '##july##', '##august##', '##september##', '##october##', '##november##', '##december##' ),
			'year' => array ( date ( 'Y' ) - 1 => date ( 'Y' ), date ( 'Y' ) => date ( 'Y' ) + 1 )
		);

		if( $this->getProperty( 'year_start' ) > 0 && $this->getProperty( 'year_end' ) > 0 ) {
			$selects[ 'year' ] = array( );
			for( $i = $this->getProperty( 'year_start' ); $i <= $this->getProperty( 'year_end' ); $i++ ) {
				$selects[ 'year' ][ $i - 1 ] = $i;
			}
		} elseif( $this->getProperty( 'year_start' ) > 0 ) {
			$selects[ 'year' ] = array( );
			for( $i = $this->getProperty( 'year_start' ); $i <= date( 'Y' ) + 1; $i++ ) {
				$selects[ 'year' ][ $i - 1 ] = $i;
			}
		} elseif( $this->getProperty( 'year_end' ) > 0 ) {
			$selects[ 'year' ] = array( );
			for( $i = date( 'Y' ); $i <= $this->getProperty( 'year_end' ); $i++ ) {
				$selects[ 'year' ][ $i - 1 ] = $i;
			}
		}

		foreach ( $selects as $key => $options ) {
			$select .= '<select name="'.( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name.'['.$key.']" id="'.( $this->getFormName() != '' ? $this->getFormName() . '_' : '' ) . $this->name.'_'.$key.'"' . ( $this->getProperty( 'disabled' ) ? ' disabled="disabled"' : '' ) . '>';

			foreach ( $options as $identifier => $value ) {
				$select .= '<option value="'.($identifier + 1).'"'.( ( $identifier + 1 ) == $selected[$key] ? ' selected="selected"' : '' ).'>'.$value.'</option>';
			}

			$select .= '</select>';
		}

		$select .= '</div>'."\r\n";

		return $select;
	}
}

